/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist;

import org.danigarcia.android.blacklist.database.DataManager;
import org.danigarcia.android.blacklist.database.CallsListCursorAdapter;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Activity which shows a list with the blocked calls and lets the user
 * perform operations of ordering and deletion.
 * @author Daniel Garcia [contacto {at} danigarcia.org]
  */
public class BlockedCallsActivity extends BaseActivity 
		implements OnItemClickListener,
		DialogInterface.OnClickListener {

	private static final String TAG = BlockedCallsActivity.class.getName();
	
	private int selectedCalls = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_blocked_calls);
		// Show the Up button in the action bar.
		setupActionBar();
		configureActivity();
		loadListView(null, null);
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.blocked_calls, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
	        case R.id.menuCallsOrderByDate:
	        	loadListView(null, getOrderBy(DataManager.COLUMN_CALLS_CREATIONDATE));
	            return true;
	            
	        case R.id.menuCallsOrderByNumber:
	        	loadListView(null, getOrderBy(DataManager.COLUMN_CALLS_PHONE));
	            return true;
	            
	        case R.id.menuCallsDeleteAll:

	        	// Set ID to 0 to tell DataManager that we want to erase all data in the table
	        	selectedCalls = 0;
	        	Resources res = getResources();
	        	
	        	// Show message using the deletion message for more than one element
	        	AlertDialog alertDialog = createConfirmDialog(getString(R.string.confirm), res.getQuantityString(R.plurals.msgConfirmDelete, 2), this);
	    		alertDialog.show();
	            return true;
	        case R.id.menuCallsBack:
	        	Log.d(TAG, "menuCallsBack");
	        	finish();
	            return true;   
	        case R.id.menuCallsAbout:
	        	about();
	            return true;  
	        default:
	            return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Method invoked if the user accepts the deletion of one or all the call registers.
	 */
    @Override
    public void onClick(DialogInterface dialog, int which) {
    	
    	// Deletion of the call elements. If selectedCalls is 0, it will remove all of them.
    	// Otherwise, it will delete the item which ID is selectedCalls
        int deleted = dm.deleteCall(selectedCalls);
        
        // Show message on Toast
        Toast.makeText(BlockedCallsActivity.this, String.format(getResources().getQuantityString(R.plurals.msgRegisterDeleted, deleted, deleted),  deleted), Toast.LENGTH_SHORT).show();
        
        // Reload the list using the previous OrderBy criteria and order
        loadListView(null, orderBy + " " + direction);
    }
	
    /**
     * Method invoked when the user clicks on an element of the list, being prompted to delete the register 
     */
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, int position, long id) 
	{
		// Get the register ID
		selectedCalls = (int)id;
		Resources res = getResources();
		
		// Show message using the deletion message for one element
		AlertDialog alertDialog = createConfirmDialog(getString(R.string.confirm), res.getQuantityString(R.plurals.msgConfirmDelete, 1), this);
		alertDialog.show();
	}
	
	@Override
	protected void configureActivity()
	{
		listView = (ListView)findViewById(R.id.lvCalls);
		listView.setOnItemClickListener(this);
		dm = new DataManager(this, DataManager.DB_NAME, null, DataManager.DB_VERSION);
	}

	@Override
	protected CursorAdapter getAdapter(String filter, String orderBy) {
		cSelect = dm.getCalls(null);
		cursorAdapter = new CallsListCursorAdapter(this, cSelect);
		return cursorAdapter;
	}

}
